package fr.umlv.jee.jawp.service.aws;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ParserXML implements Serializable {
	
	/**
   * 
   */
  private static final long serialVersionUID = 1L;

  public static List<Map<String, String>> findListItem(Document doc){
		
		List<Map<String, String>> listItem = new ArrayList<Map<String, String>>();
		//<Items> tag
		
		//get All nodes included in <Items>
		
		NodeList listNodeItem = doc.getElementsByTagName("Item");
		
		Node item;
		
		for(int i = 0; i < listNodeItem.getLength(); i++){
			Map<String, String> mapProduct = new HashMap<String, String>();
			item = listNodeItem.item(i);
			NodeList itemProperty = item.getChildNodes();
			for(int j = 0; j < itemProperty.getLength(); j++){
				String itemPropertyName = itemProperty.item(j).getNodeName();
				//ASIN
				if (itemPropertyName.equals("ASIN")) {
					mapProduct.put("ASIN", itemProperty.item(j).getTextContent());
					continue;
				}
				//ItemAttributes
				if (itemPropertyName.equals("ItemAttributes")) {
					NodeList itemAttributes = itemProperty.item(j).getChildNodes();
					for(int k = 0; k < itemAttributes.getLength(); k++){
						String itemAttributeName = itemAttributes.item(k).getNodeName();
						String itemAttributeValue = itemAttributes.item(k).getTextContent();
						if(!itemAttributeName.equals("ListPrice")){
							if(mapProduct.containsKey(itemAttributeName)){
								mapProduct.put(itemAttributeName, mapProduct.get(itemAttributeName) + ", " + itemAttributeValue);
							}
							else{
								mapProduct.put(itemAttributeName, itemAttributeValue);
							}
						}
						else{ //List Price
							NodeList itemAttributesListPrice = itemAttributes.item(k).getChildNodes();
							for(int l = 0; l < itemAttributesListPrice.getLength(); l++){
								String itemPriceAttributeName = itemAttributesListPrice.item(l).getNodeName();
								String itemPriceAttributeValue = itemAttributesListPrice.item(l).getTextContent();
								if(mapProduct.containsKey("ListPrice." + itemPriceAttributeName)){
									mapProduct.put("ListPrice." + itemPriceAttributeName, mapProduct.get("ListPrice." + itemPriceAttributeName) + ", " + itemPriceAttributeValue);
								}
								else{
									mapProduct.put("ListPrice." + itemPriceAttributeName, itemPriceAttributeValue);
								}
							}
						}
					}
					continue;
				}
				//SmallImage
				if (itemPropertyName.equals("SmallImage")) {
					NodeList itemAttributes = itemProperty.item(j).getChildNodes();
					for(int k = 0; k < itemAttributes.getLength(); k++){
						String itemAttributeName = itemAttributes.item(k).getNodeName();
						String itemAttributeValue = itemAttributes.item(k).getTextContent();
						if(mapProduct.containsKey(itemAttributeName)){
							mapProduct.put("SmallImage." + itemAttributeName, mapProduct.get(itemAttributeName) + ", " + itemAttributeValue);
						}
						else{
							mapProduct.put("SmallImage." + itemAttributeName, itemAttributeValue);
						}
					}
					continue;
				}
				//MediumImage
				if (itemPropertyName.equals("MediumImage")) {
					NodeList itemAttributes = itemProperty.item(j).getChildNodes();
					for(int k = 0; k < itemAttributes.getLength(); k++){
						String itemAttributeName = itemAttributes.item(k).getNodeName();
						String itemAttributeValue = itemAttributes.item(k).getTextContent();
						if(mapProduct.containsKey(itemAttributeName)){
							mapProduct.put("MediumImage." + itemAttributeName, mapProduct.get(itemAttributeName) + ", " + itemAttributeValue);
						}
						else{
							mapProduct.put("MediumImage." + itemAttributeName, itemAttributeValue);
						}
					}
					continue;
				}
				//LargeImage
				if (itemPropertyName.equals("LargeImage")) {
					NodeList itemAttributes = itemProperty.item(j).getChildNodes();
					for(int k = 0; k < itemAttributes.getLength(); k++){
						String itemAttributeName = itemAttributes.item(k).getNodeName();
						String itemAttributeValue = itemAttributes.item(k).getTextContent();
						if(mapProduct.containsKey(itemAttributeName)){
							mapProduct.put("LargeImage." + itemAttributeName, mapProduct.get(itemAttributeName) + ", " + itemAttributeValue);
						}
						else{
							mapProduct.put("LargeImage." + itemAttributeName, itemAttributeValue);
						}
					}
					continue;
				}
				
			}
			if( mapProduct.size() != 0 )
				listItem.add(mapProduct);
		}
		return listItem;
	}

	public static String getNodeValue(Document doc, String pNodeName){
		NodeList node = doc.getElementsByTagName(pNodeName);
		return node.item(0).getTextContent().toString();
	}
}
